package com.asa.ui.J;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.ui.frame.AlgoVisHelper;

public class AlgoFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3035088527551930125L;
	private int canvasWidth;//画布的宽
	private int canvasHeight;//画布的长
	private CircleData data;//分钱数组
   public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void render(CircleData data){
		this.data=data;
		repaint();
	}
   public AlgoFrame(String title,int  canvasWidth, int canvasHeight){
	  super(title);
	  this.canvasWidth=canvasWidth;
	  this.canvasHeight=canvasHeight;
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setResizable(false);//关闭可拖动
	  setVisible(true);
	  AlgoCanvas algoCanvas = new AlgoCanvas();
	  setContentPane(algoCanvas);
	  pack();
   }
   //设置面板JPanel
   class AlgoCanvas extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6056196800598676936L;
    private  AlgoCanvas(){
    	//开启双缓存
    	super(true);
    }
	@Override
	//在面板上绘制内容
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D) g;
		AlgoVisHelper.setRenderingHints(g2d);//设置抗锯齿
		AlgoVisHelper.setStrokeWidth(g2d, 1);//设置边的宽度为5
		
		//具体绘制
		drawCircle(g2d,data.getStartX(),data.getStartY(),data.getStartR(),0);
		
		
		
		
	}
	
	
	
	
	
	private void drawCircle(Graphics2D g2d, int startX, int startY, int startR, int depth) {
		// TODO Auto-generated method stub
		if (depth==data.getDepth()||startR<1) {
			return;
		}
		if (depth%2==0) {
			AlgoVisHelper.setColor(g2d, Color.RED);
		}else {
			AlgoVisHelper.setColor(g2d, Color.lightGray);

		}
		AlgoVisHelper.fillCircle(g2d, startX, startY, startR);
		drawCircle(g2d, startX, startY, startR-data.getStep(), depth+1);
		
		
	}
	
	
	
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(canvasWidth,canvasHeight);
	}
   }
  
}
